package controller;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import model.Mode;
	
public class Controller {
	final private ArrayList<String> knownHosts = new ArrayList<String>();
	final private ArrayList<String> unknownHosts = new ArrayList<String>();
	final private ArrayList<String> allHosts = new ArrayList<String>();
	
	final private FileIO fileIO = new FileIO();
	final private Parser parser = new Parser(this);
	
	Controller(){
		
	}
	public void analyzeFiles(JFrame frame, String[] stringFiles, Mode mode) {
		knownHosts.clear();
		unknownHosts.clear();
		allHosts.clear();
		
		File[] files = fileIO.parseForFilesAndDirectories(stringFiles);
		if (files.length>0){
			parser.parseFiles(files, frame, mode);
		}

		//being a bad person
		//TODO: Be a better person
		String outputBase = stringFiles[0];
		
		if(knownHosts.size()>0){
			fileIO.setOutputFilename(outputBase+"_Resolved.txt");
			String data = "";
			for (String host : knownHosts){
				data+=host+"\n";
			}
			fileIO.confirmWrite(data, frame);
		}
		if(unknownHosts.size()>0){
			fileIO.setOutputFilename(outputBase+"_Unresolved.txt");
			String data = "";
			for (String host : unknownHosts){
				data+=host+"\n";
			}
			fileIO.confirmWrite(data, frame);
		}
		fileIO.setOutputFilename(outputBase+"_All.txt");
		String data = "";
		for (String host : allHosts){
			data+=host+"\n";
		}
		fileIO.confirmWrite(data, frame);
		
		JOptionPane.showMessageDialog(frame, "Resolving is complete");
		
	}
	public void addKnownHost(String knownHost){
		knownHosts.add(knownHost);
		allHosts.add(knownHost);
	}
	public void addUnknownHost(String unknownHost){
		unknownHosts.add(unknownHost);
		allHosts.add(unknownHost);
	}
	public String getSelectedFileOrDirectoryFullPath (){
		return fileIO.getSelectedFileOrDirectoryFullPath();
	}
	public boolean selectFile(JFrame frame) {
		return fileIO.selectInputFile(frame);
	}
	public void setInputFile(JFrame frame, String inputFileName){
		fileIO.setInputFile(frame, inputFileName);
	}
}

